﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.IO;
//using System.Runtime.Serialization.Formatters.Binary;
using System.Xml;
using System.Xml.Linq;
//using System.Xml.Serialization;
using System.ComponentModel;


namespace MyMoviesBrowserLibrary
{
    public class Settings : INotifyPropertyChanged
    {
        private static Settings singleSettingsInstance;
        public static Settings Default
        {
            set
            {
                singleSettingsInstance = value;
            }
            get
            {
                if (singleSettingsInstance == null)
                {
                    singleSettingsInstance = new Settings();
                    string file = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), CONFIG_FILE);
                    if (File.Exists(file))
                    {
                        try
                        {
                            XDocument xdoc = XDocument.Load(file);
                            XElement root = xdoc.Descendants("Settings").FirstOrDefault();
                            if(root == null)
                                throw new ApplicationException("Invalid XML File");
                            singleSettingsInstance = new Settings();
                            bool temp;
                            if (root.Element("DownloadG") != null && bool.TryParse(root.Element("DownloadG").Value, out temp))
                                singleSettingsInstance.DownloadG = temp;
                            if (root.Element("DownloadHD") != null && bool.TryParse(root.Element("DownloadHD").Value, out temp))
                                singleSettingsInstance.DownloadHD = temp;
                            if (root.Element("DownloadNotYetRated") != null && bool.TryParse(root.Element("DownloadNotYetRated").Value, out temp))
                                singleSettingsInstance.DownloadNotYetRated = temp;
                            if (root.Element("DownloadPG") != null && bool.TryParse(root.Element("DownloadPG").Value, out temp))
                                singleSettingsInstance.DownloadPG = temp;
                            if (root.Element("DownloadPG13") != null && bool.TryParse(root.Element("DownloadPG13").Value, out temp))
                                singleSettingsInstance.DownloadPG13 = temp;
                            if (root.Element("DownloadR") != null && bool.TryParse(root.Element("DownloadR").Value, out temp))
                                singleSettingsInstance.DownloadR = temp;
                            if (root.Element("HideEpisodesWithoutFiles") != null && bool.TryParse(root.Element("HideEpisodesWithoutFiles").Value, out temp))
                                singleSettingsInstance.HideEpisodesWithoutFiles = temp;
                            if (root.Element("HideSeasonsWithoutFiles") != null && bool.TryParse(root.Element("HideSeasonsWithoutFiles").Value, out temp))
                                singleSettingsInstance.HideSeasonsWithoutFiles = temp;
                            if (root.Element("MoveFile") != null && bool.TryParse(root.Element("MoveFile").Value, out temp))
                                singleSettingsInstance.MoveFile = temp;
                            if (root.Element("RenameFile") != null && bool.TryParse(root.Element("RenameFile").Value, out temp))
                                singleSettingsInstance.RenameFile = temp;
                            if (root.Element("Try1080") != null && bool.TryParse(root.Element("Try1080").Value, out temp))
                                singleSettingsInstance.Try1080 = temp;
                            singleSettingsInstance.EpisodeSearchString = root.Element("EpisodeSearchString") != null ? root.Element("EpisodeSearchString").Value : "";
                            singleSettingsInstance.MyMoviesServerName = root.Element("MyMoviesServerName") != null ? root.Element("MyMoviesServerName").Value : "";
                            singleSettingsInstance.RenameFormat = root.Element("RenameFormat") != null ? root.Element("RenameFormat").Value : "";
                            singleSettingsInstance.TrailersDownloadFolder = root.Element("TrailersDownloadFolder") != null ? root.Element("TrailersDownloadFolder").Value : "";
                            singleSettingsInstance.SavedRootFolder = root.Element("SavedRootFolder") != null ? root.Element("SavedRootFolder").Value : "";

                        }
                        catch
                        {
                            
                        }
                    }

                }
                return singleSettingsInstance;
            }
        }

        public static string DataFolder
        {
            get
            {
                return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), CONFIG_LOCATION);
            }
        }


        const string CONFIG_FILE = @"MyMoviesBrowser\MyMoviesBrowserSettings.dat";
        const string CONFIG_LOCATION = @"MyMoviesBrowser";

        private string trailersDownloadFolder;
        private string myMoviesServerName;
        private List<string> tvFolder;
        private bool downloadHD;
        private bool try1080;
        private bool downloadG;
        private bool downloadPG;
        private bool downloadPG13;
        private bool downloadR;
        private bool downloadNotYetRated;
        private string myMoviesBrwoserConnectionString2 = @"Data Source={0}\MYMOVIES;Initial Catalog='MyMoviesBrowser';Persist Security Info=True;User ID=sa;Password=t9AranuHA7";
        private string myMoviesConnectionString2 = @"Data Source={0}\MYMOVIES;Initial Catalog='My Movies';Persist Security Info=True;User ID=sa;Password=t9AranuHA7";
        private string savedRootFolder;
        private string renameFormat = "{3} - {2}";
        private string episodeSearchString = "http://www.binsearch.info/?q={0}+s{1:00}e{3:00}&max=250&adv_age=120&server=";
        private bool hideEpisodesWithoutFiles;
        private bool hideSeasonsWithoutFiles;
        private bool moveFile = true;
        private bool renameFile = true;
        private string eaxMovieCatalogFile = @"C:\Users\DC\Downloads\Eaxmc_Movie-test.xml";

        public string TrailersDownloadFolder
        {
            get { return trailersDownloadFolder; }
            set
            {
                trailersDownloadFolder = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("TrailersDownloadFolder"));
            }
        }

        public string MyMoviesServerName
        {
            get { return myMoviesServerName; }
            set
            {
                myMoviesServerName = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("MyMoviesServerName"));
            }
        }

        public string EaxMovieCatalogFile
        {
            get { return eaxMovieCatalogFile; }
            set
            {
                eaxMovieCatalogFile = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("EaxMovieCatalogFile"));
            }
        }
        
        public List<string> TVFolder
        {
            get { return tvFolder; }
            set
            {
                tvFolder = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("TVFolder"));
            }
        }

        public bool DownloadHD
        {
            get { return downloadHD; }
            set
            {
                downloadHD = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("DownloadHD"));
            }
        }

        public bool Try1080
        {
            get { return try1080; }
            set
            {
                try1080 = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("Try1080"));
            }
        }

        public bool DownloadG
        {
            get { return downloadG; }
            set
            {
                downloadG = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("DownloadG"));
            }
        }

        public bool DownloadPG
        {
            get { return downloadPG; }
            set
            {
                downloadPG = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("DownloadPG"));
            }
        }

        public bool DownloadPG13
        {
            get { return downloadPG13; }
            set
            {
                downloadPG13 = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("DownloadPG13"));
            }
        }

        public bool DownloadR
        {
            get { return downloadR; }
            set
            {
                downloadR = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("DownloadR"));
            }
        }

        public bool DownloadNotYetRated
        {
            get { return downloadNotYetRated; }
            set
            {
                downloadNotYetRated = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("DownloadNotYetRated"));
            }
        }

        public string MyMoviesBrwoserConnectionString2
        {
            get { return myMoviesBrwoserConnectionString2; }
            set
            {
                myMoviesBrwoserConnectionString2 = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("MyMoviesBrwoserConnectionString2"));
            }
        }

        public string MyMoviesConnectionString2
        {
            get { return myMoviesConnectionString2; }
            set
            {
                myMoviesConnectionString2 = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("MyMoviesConnectionString2"));
            }
        }

        public string SavedRootFolder
        {
            get { return savedRootFolder; }
            set
            {
                savedRootFolder = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("SavedRootFolder"));
            }
        }

        public string RenameFormat
        {
            get { return renameFormat; }
            set
            {
                renameFormat = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("RenameFormat"));
            }
        }

        public string EpisodeSearchString
        {
            get { return episodeSearchString; }
            set
            {
                episodeSearchString = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("EpisodeSearchString"));
            }
        }

        public bool HideEpisodesWithoutFiles
        {
            get { return hideEpisodesWithoutFiles; }
            set
            {
                hideEpisodesWithoutFiles = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("HideEpisodesWithoutFiles"));
            }
        }

        public bool HideSeasonsWithoutFiles
        {
            get { return hideSeasonsWithoutFiles; }
            set
            {
                hideSeasonsWithoutFiles = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("HideSeasonsWithoutFiles"));
            }
        }

        public bool MoveFile
        {
            get { return moveFile; }
            set
            {
                moveFile = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("MoveFile"));
            }
        }

        public bool RenameFile
        {
            get { return renameFile; }
            set
            {
                renameFile = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("RenameFile"));
            }
        }

        public void Save()
        {
            FileInfo file = new FileInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), CONFIG_FILE));
            XDocument xdoc = new XDocument();
            XElement root = new XElement("Settings");
            xdoc.Add(root);
            root.Add(new XElement("DownloadG",this.DownloadG.ToString()));
            root.Add(new XElement("DownloadHD",this.DownloadHD.ToString()));
            root.Add(new XElement("DownloadNotYetRated",this.DownloadNotYetRated.ToString()));
            root.Add(new XElement("DownloadPG",this.DownloadPG.ToString()));
            root.Add(new XElement("DownloadPG13",this.DownloadPG13.ToString()));
            root.Add(new XElement("HideEpisodesWithoutFiles",this.HideEpisodesWithoutFiles.ToString()));
            root.Add(new XElement("HideSeasonsWithoutFiles",this.HideSeasonsWithoutFiles.ToString()));
            root.Add(new XElement("MoveFile",this.MoveFile.ToString()));
            root.Add(new XElement("RenameFile",this.RenameFile.ToString()));
            root.Add(new XElement("Try1080",this.Try1080.ToString()));
            root.Add(new XElement("EpisodeSearchString",this.EpisodeSearchString));
            root.Add(new XElement("MyMoviesServerName",this.MyMoviesServerName));
            root.Add(new XElement("RenameFormat",this.RenameFormat));
            root.Add(new XElement("TrailersDownloadFolder",this.TrailersDownloadFolder));
            root.Add(new XElement("SavedRootFolder",this.SavedRootFolder));
            xdoc.Save(file.FullName);
        }


        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}
